package com.example.gokuplayalong.bases.push;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.gokuplayalong.bases.security.MD5Util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

public class Push {
    static final String appKey = "3a25624a699f8";
    static final String appSecret = "1785ce6e4fd801bd836f587e21184fda";


    public static int createPushByRids(String content, List<String> rids, JSONArray extras) {


        String msg = "{\"source\":\"webapi\",\"appkey\":\"" + appKey + "\",\"pushTarget\":{\"target\":4,\"rids\":" + rids.toString() + "},\"pushNotify\":{\"plats\":[1],\"content\":\"" + content + "\",\"type\":2,\"extrasMapList\":" + extras.toString() + ",\"customNotify\":{\"customType\":\"text 文本消息\",\"customTitle\":\"自定义类型标题\"}}}";


        String result = MD5Util.string2MD5(msg + appSecret);
        System.out.println(msg);
        int res = 0;
        try {
            res = createPush(msg, result);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return res;
    }

    public static int createPushByRids(String content, List<String> rids, JSONObject extras) {


        String msg = "{\"source\":\"webapi\",\"appkey\":\"" + appKey + "\",\"pushTarget\":{\"target\":4,\"rids\":" + rids.toString() + "},\"pushNotify\":{\"plats\":[1],\"content\":\"" + content + "\",\"type\":2,\"extrasMapList\":[" + extras.toString() + "],\"customNotify\":{\"customType\":\"text 文本消息\",\"customTitle\":\"自定义类型标题\"}}}";
        String result = MD5Util.string2MD5(msg + appSecret);

        System.out.println(msg);
        int res = 0;
        try {
            res = createPush(msg, result);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return res;
    }

    public static String bytesToHexString(byte[] bytes) {
        StringBuilder builder = new StringBuilder();
        for (byte b : bytes) {
            builder.append(String.format("%x", b));
        }
        return builder.toString();
    }


    private static int createPush(String raw, String sign) throws IOException {
        URL url = new URL("http://api.push.mob.com/v3/push/createPush");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-type", "application/json");
        connection.setRequestProperty("key", appKey);
        connection.setRequestProperty("sign", sign);
        connection.setConnectTimeout(3000);
        connection.setReadTimeout(3000);
        connection.setDoOutput(true);
        connection.setDoInput(true);

        OutputStream out = connection.getOutputStream();
        out.write(raw.getBytes("utf-8"));

        if (connection.getResponseCode() == 200) {
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line, msg = "";
            while ((line = reader.readLine()) != null)
                msg = String.format("%s%s", msg, line);
            reader.close();
            return 1;
        }
        else {
            System.out.println("推送失败" + connection.getResponseCode());
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
            String line, msg = "";
            while ((line = reader.readLine()) != null)
                msg = String.format("%s%s", msg, line);
            reader.close();
            System.out.println(msg);
            return 0;
        }

    }

}
